/*
 * Driver -packet RX- for Winbond USB2WLAN b/g WiFi device on Linux 2.6
 * Many portions based on original GPLed driver from Winbond.
 *
 * Written by Ferenc Csicsova & Karoly Kasza
 * Based on RTL8187 driver in kernel.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/usb.h>
#include <net/mac80211.h>

#include "wbusb.h"

void wbusb_rx_complete(struct urb *purb);
void wbusb_rx(struct wbusb_priv *priv);

//Start communicating with USB device
void wbusb_rx_start(struct wbusb_priv *priv)
{
	DEBUG_RX("rx_start call")
	if (priv->rx.rx_count==0) { //HE???
		priv->rx.rx_count++;
		priv->rx.EP3vm_state=VM_RUNNING;
		wbusb_rx(priv);
	}
	return;
}

u8 wbusb_rx_init(struct wbusb_priv *priv)
{
	priv->rx.EP3vm_state=VM_STOP;
	priv->rx.buffer_id=0;
	
	priv->rx.rx_urb=usb_alloc_urb(0,GFP_ATOMIC);
	return(!!priv->rx.rx_urb);
}

//MANY from old code
void wbusb_rx(struct wbusb_priv *priv)
{
	int ret;

	DEBUG_RX("rx call")
	
	priv->rx.pdrx=kmalloc(MAX_USB_RX_BUFF_SIZE,GFP_ATOMIC);
	if (!priv->rx.pdrx) {
		printk(KERN_ERR "wbusb: wbusb_rx malloc failed!\n");
		goto cleanup;
	}
	
	usb_fill_bulk_urb(priv->rx.rx_urb,
		priv->udev,
		usb_rcvbulkpipe(priv->udev,3),
		priv->rx.pdrx,
		cpu_to_le16(MAX_USB_RX_BUFF_SIZE),
		wbusb_rx_complete,
		(void *)priv);

	priv->rx.EP3vm_state=VM_RUNNING;

	ret = usb_submit_urb(priv->rx.rx_urb, GFP_ATOMIC);
	if (ret) {
		printk(KERN_ERR "wbusb: wbusb_rx USB_SUBMIT_URB failed!\n");
		goto cleanup;
	}
	return;


cleanup:
	DEBUG_RX("wbusb_rx cleanup")
	priv->rx.EP3vm_state = VM_STOP;
	priv->rx.rx_count--;
	return;
}

void wbusb_rx_complete(struct urb *purb)
{
	struct wbusb_priv *priv = (struct wbusb_priv*)purb->context;
	priv->rx.EP3vm_state = VM_COMPLETED;
	DEBUG_RX("EP3VM_complete")

	kfree(priv->rx.pdrx);
	wbusb_rx(priv);
	return;

cleanup: //For later use
	priv->rx.EP3vm_state = VM_STOP;
	priv->rx.rx_count--;
}
